package weaver.jobs.zkwz.duss;

import com.engine.common.util.ServiceUtil;
import com.engine.salary.biz.SalaryArchiveBiz;
import com.engine.salary.entity.salaryarchive.po.SalaryArchivePO;
import com.engine.salary.service.SalaryArchiveService;
import com.engine.salary.service.impl.SalaryArchiveServiceImpl;
import com.engine.salary.util.SalaryDateUtil;
import org.apache.commons.collections4.CollectionUtils;
import weaver.common.DateUtil;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;
import weaver.hrm.User;
import weaver.interfaces.schedule.BaseCronJob;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Title: ecology
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2022-12-06
 * @Description: 薪酬模块：批量更新员工薪资档案中的最后发薪日期字段（传人员卡片中的离职日期字段field27），
 * 该离职人员已在档案中手动最后发薪日期则不会自动更新,先测试环境待客户测试完迁移正式
 * @ModifyLog:
 **/

public class WeUpdatePayEndDateCronJob extends BaseCronJob {
    private SalaryArchiveBiz getSalaryArchiveMapper = new SalaryArchiveBiz();

    private SalaryArchiveService getSalaryArchiveService(User user) {
        return (SalaryArchiveService) ServiceUtil.getService(SalaryArchiveServiceImpl.class, user);
    }

    @Override
    public void execute() {
        BaseBean baseBean = new BaseBean();
        RecordSet rs = new RecordSet();
        User user = new User();
        user.setUid(1);
        try {
            String currentDate = DateUtil.getCurrentDate();
            baseBean.writeLog(">>>>>>WeUpdatePayEndDateCronJob>>>>currentDate=" + currentDate);
            List<Long> employeeIdList = new ArrayList<>();
            rs.executeQuery("select employee_id from hrsa_salary_archive where run_status='SUSPEND' and ifnull(pay_end_date,'')=''");
            while (rs.next()) {
                employeeIdList.add(Long.valueOf(rs.getString("employee_id")));
            }
            baseBean.writeLog(">>>>>>WeUpdatePayEndDateCronJob>>>>employeeIdList=" + employeeIdList.size());
            List<SalaryArchivePO> salaryArchivePOS = getSalaryArchiveService(user).listPayEndDateIsNull(employeeIdList);
            Date now = new Date();
            List<SalaryArchivePO> needUpdateSalaryArchiveList = new ArrayList();
            for (SalaryArchivePO archive : salaryArchivePOS) {
                String sql = "select id,field27 from cus_fielddata where scopeid=3 and scope='HrmCustomFieldByInfoType' and ifnull(field27,'') !='' and id=?";
                rs.executeQuery(sql, archive.getEmployeeId());
                rs.next();
                String dismissDate = Util.null2String(rs.getString("field27"));
                baseBean.writeLog(">>>>>>WeUpdatePayEndDateCronJob>>>>employeeId=" + archive.getEmployeeId());
                baseBean.writeLog(">>>>>>WeUpdatePayEndDateCronJob>>>>dismissDate=" + dismissDate);
                archive.setPayEndDate(SalaryDateUtil.dateStrToLocalDate(dismissDate));
                archive.setUpdateTime(now);
                needUpdateSalaryArchiveList.add(archive);
            }

            if (CollectionUtils.isNotEmpty(needUpdateSalaryArchiveList)) {
                getSalaryArchiveMapper.batchUpdate(needUpdateSalaryArchiveList);
            }
        } catch (Exception e) {
            baseBean.writeLog(">>>>>>WeUpdatePayEndDateCronJob>>>>Exception" + e.getMessage(), e);
        }

    }

}

